package de.idgard.android.addon.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.webkit.JsPromptResult;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import de.idgard.android.R;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.webapp.shared.dto.profile.SimpleProfileDto;

/**
 * @author pratik
 * 
 * 
 */
public class IDGPrivacyConnectSaveDialog extends Dialog {

	private EditText etBookmarkName;
	private Button btnSaveBookmark, btnCancel;
	private JsPromptResult result;
	private CheckBox cbShowInPortal;

	public IDGPrivacyConnectSaveDialog(Context context, final JsPromptResult result, final SimpleProfileDto profile) {
		super(context);
		this.result = result;

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.idgard_dialog_save_privacydata);

		etBookmarkName = (EditText) findViewById(R.id.et_profile);
		cbShowInPortal = (CheckBox) findViewById(R.id.cb_showin_portal);
		btnSaveBookmark = (Button) findViewById(R.id.btn_save_bookmark);
		btnCancel = (Button) findViewById(R.id.btn_cancel_bookmark);

		etBookmarkName.setText(profile.getName());

		btnSaveBookmark.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				profile.setName(etBookmarkName.getText().toString());
				profile.setShowInPortal(cbShowInPortal.isChecked());
				profile.setIcon(GlobalServiceRegistry.instance().getWebIconService().getFaviconAsBase64());

				GlobalServiceRegistry.instance().getBackendService().savePrivacyData(profile).fire();

				result.confirm();
				dismiss();
			}
		});

		btnCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				result.confirm();
				dismiss();
			}
		});
	}

	@Override
	public void onBackPressed() {
		result.confirm();
		super.onBackPressed();
	}

}
